<?php
/**
*date: 2016年10月8日
*/
namespace app\admin\controller;
use think\Db;
use app\common\controller\AdminBase;
class Group extends AdminBase{
		/*index*/
		public function Index(){
		    $data=Db::name('auth_group')->select();
		    $this->assign('list',$data);
			return $this->fetch();
		}		
		/*add*/
		public function Add(){
		    if(request()->isPost()){
		        $data=input('post.');
		        $data['id']=null;
		        $id=Db::name('auth_group')->insert($data,false,true);
		        if($id){
		            return "新增成功！";
		        }else{
		            return "新增失败！";
		        }
		    }
			return $this->fetch();
		}
		/*edit*/
		public function Edit(){			
		    $id=input('id');
		    $this->assign("editData",Db::name('auth_group')->where('id',$id)->find());
		    if(request()->isPost()){
		        $data=input('post.');
		            $id=Db::name('auth_group')->update($data,false,true);
		            if($id){
		                return "修改成功！";
		            }else{
		                return "修改失败！";
		            }	       
		        
		    }
		    return $this->fetch();
		}
		/*delete*/
		public function Delete(){
		 if(request()->isPost()){
		        $id=input('id');		        
		           	
		         $group_id=Db::name('auth_group_access')->column('group_id');
		         if(!in_array($id,$group_id)){
		             $result=Db::name('auth_group')->where('id',$id)->delete();
		            if($result){
		                return ['msg'=>"删除成功！"];
		            }else{
		                return ['msg'=>'删除失败！'];
		            }
		         }else{
		             return ['msg'=>'权限已分配不能删除！'];
		         }
		       
		    }else{
		        return $this->fetch();
		    }
		}
		/*授权*/
		public function group_access(){
		    $data=Db::name('auth_rule')->select();
		    $data= AdminBase::tree($data);
		    $this->assign("group",Db::name('auth_group')->where('id',input('id'))->find());
		    $this->assign('list',$data);
		    if(request()->isPost()){
		        $data=input('post.');
		        $id=Db::name('auth_group')->update($data,false,true);
		        if($id){
		            return ['msg'=>"授权成功！",'status'=>true];
		        }else{
		            return ['msg'=>"授权失败或没有更改授权！",'status'=>false];
		        }
		    
		    }
		      return $this->fetch();
		}
}
?>